

function init_webview(url,id,titleNView){
	var view = mui.preload({
		url: url,
		id: id,
		styles: {
			"render": "always",
			"popGesture": "hide",
			"bounce": "vertical",
			"bounceBackground": "#efeff4",
			"titleNView": titleNView
		}
	});
	return view;
}
function open_view(url,id,title,method,data){	
	var titleNView=init_titleNview(title);
	var view=init_webview(url,id,titleNView);
	if(!view) {
		setTimeout(function() {
			open_detail(guid);
		}, 100);
	}
	//触发页面方法
	mui.fire(view, method,data);
	view.setStyle({
		"titleNView": titleNView
	});
	setTimeout(function () {
		view.show("slide-in-right", 300);
	},150);
	
}
function init_titleNview(title){
	var titleNView = { //详情页原生导航配置
		backgroundColor: '#f7f7f7', //导航栏背景色
		titleText: title, //导航栏标题
		titleColor: '#000000', //文字颜色
		type: 'transparent', //透明渐变样式
		autoBackButton: true, //自动绘制返回箭头
		splitLine: { //底部分割线
			color: '#cccccc'
		}
	}
	return titleNView;
}
function ajaxData(url,data,callback){
	if(data==null){
		data={};
	}
	data['token']=app.getItem("user")['token'];
	//请求顶部banner信息
	mui.getJSON(url, data, function(rsp) {
		callback(rsp);
	});
}
function getValue(name){
	var user=app.getItem("user");
	return user[name];
}
function pathopen(url,id){
	mui.openWindow({
        url: url,
        id: url,
        extras:{tid:id},
        waiting: {
            autoShow: false
        }
    });
//  mui.plusReady(function() {
//              var value = plus.webview.currentWebview().tyname;
//          });
}

// 系统常量
window.set = {
	version_name: "1.2.0",
	version: 10,
	base_url: 'https://s.lxtx001.com/',
	api_url: 'https://s.lxtx001.com/api/',
	//api_url: 'http://ql.cn/api/',
	// 存储对象和读取对象
	setItem: function(name, obj) {
		obj = JSON.stringify(obj);
		localStorage.setItem(name, obj);
	},
	getItem: function(name) {
		if(localStorage.getItem(name) == 'undefined') {
			return null;
		}
		return JSON.parse(localStorage.getItem(name));
	},
	setSession: function(name, obj) {
		obj = JSON.stringify(obj);
		sessionStorage.setItem(name, obj);
	},
	getSession: function(name) {
		if(sessionStorage.getItem(name) == 'undefined') {
			return null;
		}
		return JSON.parse(sessionStorage.getItem(name));
	},
	confirm: function(title, callback) {
		layer.open({
			content: title,
			btn: ['确定', '取消'],
			yes: function(index) {
				if(index == 1) {
					callback();
				}
			}
		});
	},
	// 打开新页面需要castapp
	open: function(id) {
		mui.openWindow({
			url: id + '.html',
			id: id + '.html',
			show: {
				aniShow: 'pop-in'
			},
			createNew:false,
			styles: {
				popGesture: 'hide'
			},
			waiting: {
				autoShow: false
			}
		});
	},
	msg: function(content) {
		layer.open({
			content: content,
			skin: 'msg',
			shadeClose: false,
			time: 2 //2秒后自动关闭
		});
	},
	alert: function(content) {
		layer.open({
			content: content,
			shadeClose: false
		});
	}
};

function checkUser(status) {
	switch(status) {
		case 0:
			// 跳去认证身份
			app.msg("请先进行认证身份");
			window.location.href = "renzhen1.html";
			break;
		case 1:
		case -2:
			// 跳去认证等待
			app.msg("请先等待系统审核身份");
			window.location.href = "renzhen0.html";
			break;
		case 2:
			// 正常
			break;
		default:
			// 跳去登录
			app.msg("账号不存在，请联系客服");
			window.location.href = "login.html";
			break;
	}
}

function get_status(no) {
	switch(no) {
		case 0:
			return '挂卖中';
			break;
		case 1:
			return '审核中';
			break;
		case 2:
			return '打款中';
			break;
		case 3:
			return '收款中';
			break;
		case 4:
			return '已完成';
			break;
	}
}

function money_cate(name) {
	switch(name) {
		case 'money':
			return '可用余额';
			break;
		case 'games_money':
			return '游戏余额';
			break;
		case 'tui_money':
			return '推广余额';
			break;
	}
}

function f_mobile(mobile) {
	var start = mobile.substr(0, 3);
	var end = mobile.substr(7, 4);
	return start + '****' + end;
}

var sub = function(str, n) {
	var r = /[^\x00-\xff]/g;
	if(str.replace(r, "mm").length <= n) {
		return str;
	}
	
	var m = Math.floor(n / 2);
	for(var i = m; i < str.length; i++) {
		if(str.substr(0, i).replace(r, "mm").length >= n) {
			return str.substr(0, i) + "...";
		}
	}
	return str;
}

/*
 * ajax 函数，处理 ajax 请求
 * @param function callback 回调函数
 * @因为要和服务器交互，所以必须在服务器环境运行，不能在硬盘上直接打开
 */
function _ajax(callback) {
	if(typeof callback != 'function') return;
	var ajaxObject;
	try {
		ajaxObject = new XMLHttpRequest();
	} catch(e) {
		try {
			ajaxObject = new ActiveXObject('Microsoft.XMLHTTP');
		} catch(e) {}
	}
	if(!ajaxObject) return;
	if(ajaxObject.overrideMimeType) {
		ajaxObject.overrideMimeType('text/html');
	}
	//location.href可以换成其他url，但必须是同一个站点的链接，并且文件存在
	ajaxObject.open('get', location.href);
	ajaxObject.send(null);
	ajaxObject.onreadystatechange = function() {
		if(ajaxObject.readyState == 4) {
			if(ajaxObject.status == 200) {
				callback(ajaxObject);
			}
		}
	};
}